/**
 * Created by walkman on 2017/6/25.
 */
'user strict';
(function () {

  angular.module('WfbPlatformApp').controller('TeamRecordController',
    ['$rootScope', '$scope', '$http', '$compile', 'http', '$q', 'teamService',
      function ($rootScope, $scope, $http, $compile, http, $q, teamService) {

        // 新增参数
        $scope.flag = true;
        $scope.pageSize = 10; //页面大小
        $scope.selPage = 1;   //当前页面
        $scope.Records = {};

        //设置当前选中页样式
        $scope.isActivePage = function (page) {
          return $scope.selPage == page;
        };

        $scope.getInvestRecord = function (pageIdx, pageSize) {
          var promise = teamService.getInvestRecord(
            {
              pageNo: pageIdx,
              pageSize: pageSize
            });
          promise.then(function success(result) {
            //成功回调
            if (result.retCode == '1') {
              var json = result.data;
              //数据渲染
              $scope.Records = json;
              $scope.pages = Math.ceil(result.totalCount / $scope.pageSize); //分页数
              $scope.flag && firstLoad();
              $scope.isActivePage($scope.selPage);
            }
          }, function error(error) {//错误回调
          });
        };

        //首次加载的页码样式 - 新增函数 -
        function firstLoad() {
          $scope.newPages = $scope.pages > 5 ? 5 : $scope.pages;
          $scope.pageList = [];
          for (var i = 0; i < $scope.newPages; i++) {
            $scope.pageList.push(i + 1);
          }
        }

        //打印当前选中页索引
        $scope.selectPage = function (page) {
          //不能小于1大于最大
          if (page < 1 || page > $scope.pages) {
            return;
          }
          //最多显示分页数5
          if (page > 2) {
            //因为只显示5个页数，大于2页开始分页转换
            var newpageList = [];
            for (var i = (page - 3); i < ((page + 2) > $scope.pages ? $scope.pages : (page + 2)); i++) {
              newpageList.push(i + 1);
            }
            $scope.pageList = newpageList;
          }
          $scope.selPage = page;
          $scope.getInvestRecord($scope.selPage, $scope.pageSize);
        };

        //上一页
        $scope.Previous = function () {
          $scope.selectPage($scope.selPage - 1);
        };

        //下一页
        $scope.Next = function () {
          $scope.selectPage($scope.selPage + 1);
        };

        $scope.getInvestRecord($scope.selPage, $scope.pageSize);

      }]
  );

})();



